﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;

public partial class System_RoleMenus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ActionTitle.Text = Role.GetRole(Request.QueryString["RoleID"]).RoleName + "菜单设置";
            BindMenuList();
            BindUserMenuList();
        }
    }
    private void BindMenuList()
    {
        MenuList.Items.Clear();
        List<MenuInfo> menus = Yipusoft.Account.BLL.Menu.GetList("");
        List<MenuInfo> roleMenus = Role.GetRoleMenuList(Request.QueryString["RoleID"]);
        ListItem li;
        foreach (MenuInfo menu in menus)
        {
            if (!roleMenus.Contains(menu))
            {
                li = new ListItem();
                li.Text = "".PadRight(2 * menu.MenuLevel, '　') + menu.MenuName;
                li.Value = menu.MenuID;
                MenuList.Items.Add(li);
            }
        }
    }
    private void BindUserMenuList()
    {
        RoleMenuList.Items.Clear();
        List<MenuInfo> menus = Role.GetRoleMenuList(Request.QueryString["RoleID"]);
        ListItem li;
        foreach (MenuInfo menu in menus)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * menu.MenuLevel, '　') + menu.MenuName;
            li.Value = menu.MenuID;
            RoleMenuList.Items.Add(li);
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        if (MenuList.SelectedValue != "")
        {
            foreach (ListItem li in MenuList.Items)
            {
                if (li.Selected)
                    AddMenu(li.Value);
            }
            BindMenuList();
            BindUserMenuList();
        }
        else
            Prompt.Text = "警告：请选择一个权限！";
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        if (RoleMenuList.SelectedValue != "")
        {
            foreach (ListItem li in RoleMenuList.Items)
            {
                if (li.Selected)
                    Role.RemoveMenu(Request.QueryString["RoleID"], li.Value);
            }
            BindMenuList();
            BindUserMenuList();
        }
        else
            Prompt.Text = "警告：请选择一个权限！";
    }
    private void AddMenu(string menuID)
    {
        MenuInfo menu = Yipusoft.Account.BLL.Menu.GetMenu(menuID);
        Role.AddMenu(Request.QueryString["RoleID"], menuID);
        if (menu.ParentID != "")
            AddMenu(menu.ParentID);
    }
}
